#include "detectorprotocol.h"
#include "detectorsendpacket.h"
#include "detectorparsepacket.h"
#include "detectorprotocoldata.h"
#include "detectorparsesystempacket.h"
#include "detectorsendsystempacket.h"
#include "common/makepacket.h"

#include <QVariant>
#include "modulehplccmdid.h"

namespace hplc
{
namespace qinfine
{

DetectorProtocol::DetectorProtocol(DetectorParam &param, QObject *parent)
    : DetectorProtocol (*new DetectorProtocolData(), param, parent)
{

}

DetectorProtocol::DetectorProtocol(DetectorProtocolData &data, DetectorParam &param, QObject *parent)
    : ProtocolCommon(data, param, parent)
    , m_param(&param)
{
    DetectorParsePacket *parser = new DetectorParsePacket(data, param, this);
    DetectorSendPacket *sender = new DetectorSendPacket(data, this);
    connect(sender, &SendCommonPacket::requestSend, this, &ProtocolCommon::requestSend);

    DetectorParseSystemPacket *systemParser = new DetectorParseSystemPacket(data, param, this);
    DetectorSendSystemPacket *systemSender = new DetectorSendSystemPacket(data);

    data.m_parser = parser;
    data.m_sender = sender;
    data.m_systemParser = systemParser;
    data.m_systemSender = systemSender;
}

DetectorProtocol::~DetectorProtocol()
{
    DetectorProtocolData *d = static_cast<DetectorProtocolData*>(m_d);
    delete d->m_sender;
    delete d->m_parser;
    delete d->m_systemParser;
    delete d->m_systemSender;
}

bool DetectorProtocol::setProtocolCmd(int id, QVariant &var)
{
    return ProtocolCommon::setProtocolCmd(id, var);
}

bool DetectorProtocol::getProtocolCmd(int id, QVariant &var)
{
    return ProtocolCommon::getProtocolCmd(id, var);
}

DetectorSendPacket *DetectorProtocol::getDetectorSender()
{
    return static_cast<DetectorSendPacket*>(m_d->m_sender);
}

DetectorSendSystemPacket *DetectorProtocol::getDetectorSystemSender()
{
    DetectorProtocolData *m_d = static_cast<DetectorProtocolData*>(this->m_d);
    return m_d->m_systemSender;
}

void DetectorProtocol::setModel(quint8 model)
{
    m_param->deviceParam.model = static_cast<hplc::DetectorModel>(model);

    QVariant var;
    var.setValue(m_param->deviceParam.model);
    emit parseCompleted(hplc::qinfine::CommonSystemCmdID_SetModel, var);
}

} // qinfine
} // hplc
